package com.espresso.espressodemo;

import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.longClick;
import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


/**
 * Created by lenov on 2015/7/10.
 */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class MenuActivityTest extends ActivityInstrumentationTestCase2<MenuActivity> {

    Activity mActivity;

    public MenuActivityTest() {
        super(MenuActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();

        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void testPopupMenu() {
        onView(withText(R.string.popup_item_1_text)).check(doesNotExist());
        onView(withId(R.id.popup_button)).perform(click());
        //onView(withId(R.id.text_context_menu)).perform(click());
        onView(withText(R.string.popup_item_1_text)).check(matches(isDisplayed())).perform(click());
        onView(withId(R.id.text_menu_result)).check(matches(withText(R.string.popup_item_1_text)));

    }
    @Test
    public void testContextMenu() {
        onView(withText(R.string.context_item_2_text)).check(doesNotExist());
        onView(withId(R.id.text_context_menu)).perform(longClick());
        onView(withText(R.string.context_item_2_text)).check(matches(isDisplayed())).perform(click());
        onView(withId(R.id.text_menu_result)).check(matches(withText(R.string.context_item_2_text)));
    }
    @Test
    public void testShowText() {
        onView(withId(R.id.goto_button)).perform(click());
        onView(withId(R.id.et_text))
                .perform(typeTextIntoFocusedView("haha"), closeSoftKeyboard());
        onView(withId(R.id.btn_text)).perform(click());
        onView(withId(R.id.text_menu_result))
                .check(matches(withText(("haha"))));
//        onView(withId(R.id.text_context_menu)).perform(click());
//        onView(withId(R.id.text_context_menu)).perform(click());
//        onView(withId(R.id.text_context_menu)).perform(click());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

}
